/**
 * @BaseUIView.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2010 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:TribeGameClient
 * <br>Date:2012-3-23
 */
package com.asframe.astruts2.mvc.view
{
	import com.asframe.collections.maps.HashMap;
	import com.asframe.collections.maps.IMap;
	import com.asframe.utils.CommonTools;
	import com.asframe.utils.RefUtils;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	
	/**
	 *
	 * @author sodaChen
	 * @Date:2012-3-23
	 */
	public class BaseUIView extends MvcViewAdapter
	{
		/** 释放界面事件 **/
		public static const DISPOSE_VIEW:String = "disposeView";
		/** 单纯地关闭界面 **/
		public static const CLOSE_VIEW_EVENT:String = "closeViewEvent";
		
		
		/** 是否已经初始化界面了 **/
		public var isInitView:Boolean;
		/** 关闭是否作为释放整个view资源,默认是true **/
		public var isCloseDispose:Boolean;
		public var applicationDomain:ApplicationDomain;
		
		/** 具备关闭按钮(X) **/
		private var closeBtn:SimpleButton;
		/** 具备文字的关闭按钮 **/
		private var titleCloseBtn:SimpleButton;
		
		private var btnFunMap:IMap;
		
		public function BaseUIView()
		{
		}
		/**
		 * 创建一个图形按钮(适合文字大小不会变的按钮)
		 * @param wordBM:文字的链接
		 * @param bx:按钮x坐标
		 * @param by:按钮y坐标
		 * @param bw:按钮宽
		 * @param bh:按钮高
		 * @return 
		 * 
		 */		
		public function createButton(
												link:String,
												bx:Number = 0,
												by:Number = 0,
												bw:Number = 0,
												bh:Number = 0,
												onClick:Function = null,
												btnname:String = null):SimpleButton
		{
			var button:SimpleButton = RefUtils.refButton(link,bw,bh,bx,by);
			if(viewContainer != null)
			{
				viewContainer.addChild(button);
			}
			if(onClick != null)
			{
				button.addEventListener(MouseEvent.CLICK,onClick,false,0,true);
				if(btnFunMap == null)
				{
					btnFunMap = new HashMap();
				}
				//存放，到时可以自动清除
				btnFunMap.put(button,onClick);
			}
			return button;
		}
		/**
		 * 初始化界面相关的数据，一般是有框架自动调用 
		 */		
		public override function init():void
		{
			isCloseDispose = true;
			isInitView = true;
			onInit();
		}
		public override function dispose(o:* = null):void
		{
			if(closeBtn != null)
			{
				closeBtn.removeEventListener(MouseEvent.CLICK,onCloseEvent);
				closeBtn.parent.removeChild(closeBtn);
				closeBtn = null;
			}
			if(titleCloseBtn != null)
			{
				titleCloseBtn.removeEventListener(MouseEvent.CLICK,onCloseEvent);
				titleCloseBtn.parent.removeChild(titleCloseBtn);
				titleCloseBtn = null;
			}
			isInitView = false;
			onDispose();
			if(btnFunMap != null)
			{
				var buttons:Array = btnFunMap.valuesToArray();
				//清除通过createButton注册的事件
				for each(var btn:SimpleButton in buttons)
				{
					var onClick:Function = btnFunMap.get(btn) as Function;
					btn.removeEventListener(MouseEvent.CLICK,onClick);
					if(btn.parent == viewContainer)
					{
						viewContainer.removeChild(btn);
					}
				}
				btnFunMap.clear();
				btnFunMap = null;
			}
		}
		/**
		 * 初始化界面相关回调函数，子类重写 
		 */		
		protected function onInit():void
		{
			
		}
		/**
		 *  释放view的相关资源，子类重写。释放自己特有的资源
		 */		
		protected function onDispose():void
		{
			
		}
		private function onCloseEvent(event:MouseEvent):void
		{
			getContainer().dispatchEvent(new Event(CLOSE_VIEW_EVENT));
		}
	}
}
